package Engine.Tools;

import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;

import Engine.Funktions;
import Engine.PhysicEngine.PlayerController;
import Engine.World.World;
import static org.lwjgl.opengl.GL11.*;

public class EnterHaken extends BasicTool {

	Texture texture;
	Texture MouseCursor;
	private World terrain;

	public EnterHaken(PlayerController player) {
		super(player);
		haken = new Haken(10, 10, 10, 10, 10);
		haken.FreezeRotation = false;
		_player.engine.AddRenderComponent(haken);
		Mouse.setGrabbed(true);

	}

	float angle = 0;
	int xCur, yCur;

	@Override
	public void RenderObject_gui() {
		// TODO Auto-generated method stub
		xCur = (int) (Mouse.getX() / _player.engine.zoom);
		yCur = (int) ((Display.getHeight() - Mouse.getY()) / _player.engine.zoom);

		xCur += ((_player.engine.getCameraxPos()) + (Display.getWidth() - ((1 / _player.engine.zoom) * Display
				.getWidth())) * 0.5);
		yCur += ((_player.engine.getCamerayPos()) + (Display.getHeight() - ((1 / _player.engine.zoom) * Display
				.getHeight())) * 0.5);
		glPushMatrix();

		glTranslatef(xCur - MouseCursor.getTextureWidth() / 16, yCur
				- MouseCursor.getTextureHeight() / 16, 0);

		glTranslatef(MouseCursor.getTextureWidth() / 16,
				MouseCursor.getTextureWidth() / 16, 0);

		glRotatef(angle + 90, 0, 0, 1);
		glTranslatef(-MouseCursor.getTextureWidth() / 16,
				-MouseCursor.getTextureWidth() / 16, 0);

		DrawImage(MouseCursor, 0, 0, MouseCursor.getTextureWidth() / 8,
				MouseCursor.getTextureHeight() / 8, 20);

		glPopMatrix();
	}

	Haken haken;

	boolean isButton0down = false;

	@Override
	public void BasicTool_draw() {

		glPushMatrix();

		double Gegen, An;
		Gegen = yCur - (_player.getyPosition() + 8);
		An = xCur - (_player.getxPosition() + 10);
		angle = (float) Math.atan(Gegen / An);
		if (An < 0) {
			angle += Math.PI;
		}
		angle = (float) (angle * 180 / Math.PI);

		if (Mouse.isButtonDown(0)) {
			if (!isButton0down) {
				haken.FreezePosition = false;
				haken.FreezeRotation = false;
				haken.setLuftwiederstand(0);
				haken.setGravitation(5);
				haken.setyGeschwindigkeit(Gegen / 2);
				haken.setxGeschwindigkeit(An / 2);
				haken.setxPosition(_player.GetXPosition());
				haken.setyPosition(_player.GetYPosition());
			}
			haken.IsVisible = true;

			isButton0down = true;
		} else {
			isButton0down = false;
		}

		glTranslatef(0 + 10, +(texture.getTextureHeight() / 64), 0);

		glRotatef(angle, 0, 0, 1);

		glTranslatef(0, -texture.getTextureHeight() / 64, 0);

		DrawImage(texture, 0, 0, texture.getTextureWidth() / 32,
				texture.getTextureHeight() / 32, 20);

		glPopMatrix();

	}

	@Override
	public void BasicTool_init() {
		texture = Funktions.loadTexture("Tools/Enter.png", GL11.GL_NEAREST);
		MouseCursor = Funktions
				.loadTexture("Tools/Curser.png", GL11.GL_NEAREST);
		haken.init(_player.engine._physic, 10, 10);
	}

	public World getTerrain() {
		return terrain;
	}

	public void setTerrain(World terrain) {
		this.terrain = terrain;
	}

}
